﻿#include <QWidget>
#include <QApplication>
#include <QFile>
#include <QPushButton>
#include <QDebug>

#include "qlua.h"
#include "runslot.h"
#include "dialog.h"

static int connect(lua_State* state)
{
    QObject * a = (QObject*)tolua_tousertype(state, 1, 0);
    const char * signal = tolua_tostring(state, 2, 0);
    QObject * b = (QObject*)tolua_tousertype(state, 3, 0);
    const char * slot = tolua_tostring(state, 4, 0);

    QObject::connect(a, QString("2%0").arg(signal).toLocal8Bit().data(),
                     b, QString("1%0").arg(slot).toLocal8Bit().data());

    return 1;
}

static int testPrint(lua_State* state)
{
    printf("C++ Function\n");

    return 1;
}

static int exec(lua_State *state)
{
    QObject* obj = (QObject* )tolua_tousertype(state, 1, 0);
    const char * slot = tolua_tostring(state, 2, 0);

    if(obj != NULL)
    {
        RunSLOT run;
        QObject::connect(&run, SIGNAL(sendSignal()),
                obj, QString("1%0()").arg(slot).toLocal8Bit().data());
    }

    return 1;
}

static int QObject_delete(lua_State* state)
{
    qDebug() << "delete Start";
    QObject* obj = (QObject* )tolua_tousertype(state, 1, 0);
    if(NULL != obj)
    {
        qDebug() << "delete~";
        delete obj;
    }
    return 1;
}

static int QPushButton_delete(lua_State* state)
{
    qDebug() << "delete Start";
    QObject* obj = (QObject* )tolua_tousertype(state, 1, 0);
    if(NULL != obj)
    {
        qDebug() << "delete~";
        delete obj;
    }
    return 1;
}

static int QWidget_new(lua_State* state)
{
    QWidget* widget = new QWidget();
    tolua_pushusertype(state, widget, "QWidget");
    return 1;
}

static int QPushButton_new(lua_State* state)
{
    QPushButton* button = new QPushButton();
    tolua_pushusertype(state, button, "Button");
    return 1;
}

static int QWidget_resize(lua_State* state)
{
    QWidget* widget = (QWidget* )tolua_tousertype(state, 1, 0);
    double a = tolua_tonumber(state, 2, 0);
    double b = tolua_tonumber(state, 3, 0);

    if(widget)
    {
        widget->resize((int)a, (int)b);
    }
    return 1;
}

int main(int argc, char * argv[])
{
    QApplication a(argc, argv);

    /*
    QLua lua;

    lua.beginModule("");
    lua.addType("QWidget", QObject_delete);
    lua.moduleTypeFunction("new", QWidget_new);
    lua.moduleTypeFunction("resize", QWidget_resize);
    lua.moduleTypeFunction("exec", exec);
    lua.endModule();

    lua.addType("Button", QPushButton_delete);
    lua.moduleTypeFunction("new", QPushButton_new);
    lua.moduleTypeFunction("exec", exec);
    lua.endModule();
    lua.endModule();

    lua.pushFunction("connect", connect);
    lua.pushFunction("testPrint", testPrint);

    QFile file("C:\\360Disk\\QtLua\\LuaTest\\test.lua");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&file);
    in.setCodec("UTF-8");
    QString code = in.readAll();
    lua.run(code);
    qDebug() << "Finished";*/

    Dialog dialog;
    dialog.show();

    return a.exec();
}
